#coding=utf-8
#@Time:2020/5/14 10:17 AM
#@Author:希哥
#@File:auto_product.py
import time
import sql
import schedule
import producr_delay
import dingding


def job():
    data_sql = "SELECT product_id,do_date,do_type,operator FROM xige_hp_test.auto_sale_product WHERE dt = '0'"
    data = sql.search_total(data_sql, 'hupo_online')
    for i in range(0,len(data)):
        now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
        now_to_m = time.strftime("%Y-%m-%d %H:%M:00", time.localtime(time.time()))
        product_id = data[i][0]
        date = data[i][1]
        type = data[i][2]
        operator = data[i][3]
        product_title_sql = "SELECT title FROM product WHERE id = '" + product_id + "'"
        product_title = sql.search(product_title_sql, 'hupo_online')
        if type == 'on':
            new_type = '上架'
        if type == 'off':
            new_type = '下架'
        try:
            if producr_delay.is_today(str(date)) == True:
                producr_delay.auto_sale_product(product_id, type, date, operator)
                print now, product_id, date, new_type + '处理完毕'
                msg = '任务提示：' + str(now) + '，产品id：' + product_id + '，商品标题：' + product_title + '，' + new_type + '成功，操作人：' + operator
                dingding.DingTalk_Base().send_msg(msg)
            else:
                print now,product_id,new_type, date, '此商品的处理时间不是当前时间段，暂不处理'

        except:
            msg = '任务提示：' + str(now) + '，产品id：' + product_id + '，商品标题：' + product_title + '，' + new_type + '有记录配置错误，请修改！或者联系希哥！操作人：' + operator
            dingding.DingTalk_Base().send_msg(msg)

def stock_job():
    data_sql = "SELECT product_id,stock,operator FROM xige_hp_test.product_check_stock"
    data = sql.search_total(data_sql, 'hupo_online')
    for i in range(0, len(data)):
        now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
        product_id = data[i][0]
        stock = data[i][1]
        operator = data[i][2]
        producr_delay.check_product_stock(product_id, stock, operator)


def run():
    #schedule.every(15).minutes.do(job)  # 每隔15分钟执行一次任务
    #schedule.every().hour.do(job)  # 每隔一小时执行一次任务
    #schedule.every().day.at("10:00").do(job)  # 每天的10:30执行一次任务
    #上下架定时任务
    schedule.every().day.at("00:00").do(job)
    schedule.every().day.at("01:00").do(job)
    schedule.every().day.at("02:00").do(job)
    schedule.every().day.at("03:00").do(job)
    schedule.every().day.at("04:00").do(job)
    schedule.every().day.at("05:00").do(job)
    schedule.every().day.at("06:00").do(job)
    schedule.every().day.at("07:00").do(job)
    schedule.every().day.at("08:00").do(job)
    schedule.every().day.at("09:00").do(job)
    schedule.every().day.at("10:00").do(job)
    schedule.every().day.at("11:00").do(job)
    schedule.every().day.at("12:00").do(job)
    schedule.every().day.at("13:00").do(job)
    schedule.every().day.at("14:00").do(job)
    schedule.every().day.at("15:00").do(job)
    schedule.every().day.at("16:00").do(job)
    schedule.every().day.at("17:00").do(job)
    schedule.every().day.at("18:00").do(job)
    schedule.every().day.at("19:00").do(job)
    schedule.every().day.at("20:00").do(job)
    schedule.every().day.at("21:00").do(job)
    schedule.every().day.at("22:00").do(job)
    schedule.every().day.at("23:00").do(job)
    #商品库存报警定时任务
    schedule.every().day.at("00:00").do(stock_job)
    schedule.every().day.at("01:00").do(stock_job)
    schedule.every().day.at("02:00").do(stock_job)
    schedule.every().day.at("03:00").do(stock_job)
    schedule.every().day.at("04:00").do(stock_job)
    schedule.every().day.at("05:00").do(stock_job)
    schedule.every().day.at("06:00").do(stock_job)
    schedule.every().day.at("07:00").do(stock_job)
    schedule.every().day.at("08:00").do(stock_job)
    schedule.every().day.at("09:00").do(stock_job)
    schedule.every().day.at("10:00").do(stock_job)
    schedule.every().day.at("11:00").do(stock_job)
    schedule.every().day.at("12:00").do(stock_job)
    schedule.every().day.at("13:00").do(stock_job)
    schedule.every().day.at("14:00").do(stock_job)
    schedule.every().day.at("15:00").do(stock_job)
    schedule.every().day.at("16:00").do(stock_job)
    schedule.every().day.at("17:00").do(stock_job)
    schedule.every().day.at("18:00").do(stock_job)
    schedule.every().day.at("19:00").do(stock_job)
    schedule.every().day.at("20:00").do(stock_job)
    schedule.every().day.at("21:00").do(stock_job)
    schedule.every().day.at("22:00").do(stock_job)
    schedule.every().day.at("23:00").do(stock_job)
    #schedule.every(5).to(10).days.do(job, name)  # 每隔5到10天执行一次任务
    #schedule.every().monday.do(job, name)  # 每周一的这个时候执行一次任务
    #schedule.every().wednesday.at("13:15").do(job, name)  # 每周三13:15执行一次任务

    while True:
        schedule.run_pending()  # run_pending：运行所有可以运行的任务
        time.sleep(1)

run()